//
//  CYMyDefaultCell.m
//  CreditAddressBook
//
//  Created by Lee on 15/8/21.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYMyDefaultCell.h"
#import "CYPerson.h"
#import "UIImageView+WebCache.h"

@interface CYMyDefaultCell()

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *iconBtn;
@end

@implementation CYMyDefaultCell

- (void)setPerson:(CYPerson *)person {
    _person = person;
    
    self.nameLabel.text = person.simpleRecord.name;
    self.phoneLabel.text = person.simpleRecord.phone;
    
    if (person.simpleRecord.headPic && ![person.simpleRecord.headPic isEqualToString:@""]) {
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:person.simpleRecord.headPic] placeholderImage:[UIImage imageNamed:@"ic_contact_info_header"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
    } else {
        [self.headImageView setImage:[UIImage imageNamed:@"ic_contact_info_header"]];
    }
    
    self.iconBtn.hidden = !person.show;
    if (person.show) {
        if (person.iconType == CYIconTypeYS) {
            [self.iconBtn setBackgroundImage:[UIImage imageWithName:@"icon_ys50"] forState:UIControlStateNormal];
        } else {
            [self.iconBtn setBackgroundImage:[UIImage imageWithName:@"icon_sx50"] forState:UIControlStateNormal];
        }
    } else {
        // [self loadYSX:person];
    }
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"newCell";
    CYMyDefaultCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CYMyDefaultCell" owner:nil options:nil] lastObject];
    }
    return cell;
}

- (void)iconBtnAddTarget:(id)target action:(SEL)action {
    [self.iconBtn addTarget:target action:action forControlEvents:UIControlEventTouchDown];
}

- (void)awakeFromNib {
    self.headImageView.layer.cornerRadius = 20;
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.borderColor = [CYNaviThemeColor CGColor];
    self.headImageView.layer.borderWidth = 1;
}

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}
@end
